﻿using System;
using System.Collections.Generic;
using System.Text;
using HBCONTROLMODLib;
using System.Configuration;

namespace housebotplugin
{
    /// <summary>
    /// Plugin usage in LCDSmartie
    /// $dll(housebotplugin,1,device_name,property_name) - This function will display device values from housebot
    /// $dll(housebotplugin,2,mode_name,) - This function will display mode state from housebot
    /// </summary>
    public class LCDSmartie
    {
        const int RETRY = 3;
        int iLCDSize = 0;

        HBControl hb;

        public LCDSmartie()
        {
            //set correct config file
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "plugins\\housebotplugin.dll.config");
            Connect();
        }

        // This function is used in LCDSmartie by using the dll command as follows:
        //    $dll(housebotplugin,1,device_name,property_name)
        // This function will display device values from housebot
        public string function1(string device_name, string property_name)
        {
            try
            {
                string newValue = "";
                string response = "";
                for (int r = 0; r < RETRY; r++)
                {
                    //get value from housebot
                    newValue = hb.GetPropertyValue(device_name, property_name);

                    //check that there is not an error
                    if (!newValue.Equals("**Error**"))
                    {
                        //looks ok, so returning housebot value
                        return newValue;
                    }
                    else
                    {
                        //there is a problem, so attempts to connect first
                        response = Connect();
                    }
                }
                //still can not connect, so return response message
                return "**Error: " + response;
            }
            catch (Exception ex)
            {
                return "**Error: " + ex.Message;
            }
        }
        // This function is used in LCDSmartie by using the dll command as follows:
        //    $dll(housebotplugin,2,mode_name,)
        // This function will display mode state from housebot
        public string function2(string mode_name, string notused)
        {
            try
            {
                string newValue = "";
                string response = "";
                for (int r = 0; r < RETRY; r++)
                {
                    //get value from housebot
                    newValue = hb.GetModeState(mode_name);

                    //check that there is not an error
                    if (!newValue.Equals("**Error**"))
                    {
                        //looks ok, so returning housebot value
                        return newValue;
                    }
                    else
                    {
                        //there is a problem, so attempts to connect first
                        response = Connect();
                    }
                }
                //still can not connect, so return response message
                return "**Error: " + response;
            }
            catch (Exception ex)
            {
                return "**Error: " + ex.Message;
            }
        }
        // This function is used in LCDSmartie by using the dll command as follows:
        //    $dll(housebotplugin,3,device_name.property_name,line number)
        // This function will display device alpha list values from housebot
        public string function3(string device_namepropery, string linenumber)
        {
            try
            {
                string newValue = "";
                string response = "";
                string[] device = device_namepropery.Split(char.Parse("."));
                for (int r = 0; r < RETRY; r++)
                {
                    //get value from housebot
                    newValue = hb.GetPropertyValue(device[0], device[1]);

                    //check that there is not an error
                    if (!newValue.Equals("**Error**"))
                    {
                        //looks ok, so returning housebot value
                        if (newValue.Contains("\r"))
                        {
                            string[] lines = newValue.Split(char.Parse("\r"));
                            return lines[int.Parse(linenumber) + 1];
                        }
                        else
                        {
                            return newValue;
                        }
                    }
                    else
                    {
                        //there is a problem, so attempts to connect first
                        response = Connect();
                    }
                }
                //still can not connect, so return response message
                return "**Error: " + response;
            }
            catch (Exception ex)
            {
                return "**Error: " + ex.Message;
            }

        }
        // Define the minimum interval that a screen should get fresh data from our plugin.
        // The actual value used by Smartie will be the higher of this value and of the "dll check interval" setting
        // on the Misc tab.  [This function is optional, Smartie will assume 300ms if it is not provided.]
        public int GetMinRefreshInterval()
        {
            return 1000; // 1 second
        }
        private string Connect()
        {
            try
            {
                //disconnect all connection first
                hb = null;

                //declaire new connection and connect
                hb = new HBControl();
                string response = hb.Connect(
                int.Parse(GetValueFromConfig("ServerPort")),
                GetValueFromConfig("ServerName"),
                GetValueFromConfig("ServerPassword"));
                if (response == null)
                    return "Unable to connect to HouseBot";
                else
                    return response;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        private string GetValueFromConfig(string value)
        {
            try
            {
                return ConfigurationManager.AppSettings[value];
            }
            catch
            {
                return "";
            }
        }
    }
}
